<script type="text/javascript">
    $.extend({
        refresh_device_list:function() {
        if ($("#load_device_tip").hasClass('load')) {
                $('#grid_disk_table').trigger("reloadGrid");
            }
            else
            {
               clearInterval(window.interval_device_list)
            }
        }
    });
    
    if (window.interval_device_list) {
    } else {
        window.interval_device_list = window.setInterval("$.refresh_device_list()", 60000);
    }


    {% if raid_missing_hostids %}
        var hostips = "{{raid_missing_hostips}}";
        angular.element('#disk_modal_controller').scope().open('raid_missing_confirm?hostids={{raid_missing_hostids}}&hostips='+hostips);
    {% endif %}

                
    $("#load_device_tip").attr("for","true")
    function raid_add(disk_id, force){
        var temp=$("#raid_add_modal").find(".add_raid_disk").text(); 
        var is_read = $('#raid_add_modal').find("#is_join_raid").is(':checked');
        angular.element('#disk_modal_controller').scope().close();
//        $(".device_tip").text(temp+"磁盘加入RAID中，请耐心等待").css({"display":"block","color":'#000'});                                              			       

        $("#disk_path").addClass(disk+"add_raid");
        var disk_row_id=$('#grid_disk_table').find("td[aria-describedby=grid_disk_table_device]:contains('"+disk+"')").parent().attr("id");
        $.post("/disk/raid_add", 
                {"force": force, "disk_id": disk_id},
            function(data) {
                if (data.reply.is_success) {
                    $(".device_tip").text(temp+"加入RAID成功").css({"display":"block","color":'#000'}); 
                    $('#grid_disk_table').trigger("reloadGrid"); 
                    $("#rqDropDownList_disk_list_"+ disk_row_id).remove();               
                } else {
                    var error =  data.reply.error;
                    if (isContains(error, "try use --force") || isContains(error, 'Read-only file system')) {
                        angular.element('#disk_modal_controller').scope().open('/disk/raid_add_force_confirm?disk_id='+disk_id);
                    } else {
                        error =  data.reply.error;
                        $(".device_tip").text(error).css({"display":"block","color":'#000'});             		       
                    }
                }
        })  
    }

    var initial_width;
    $('#grid_disk_table').jqGrid({
        url:'/disk/grid',
//        postData: postData,
        datatype: 'json',
        mtype: 'GET',
        styleUI : 'Bootstrap',
        colNames: ['编号' ,"[[ 'HOST'|translate ]]", '[[ "label path"|translate ]]','used','[["label capacity"|translate]]',
                'capacity','挂载点',
                '[["label whether data"|translate]]','isjoin','host_id','islight','[["label state"|translate]]',"是否可删除",
                '[["label operation"|translate]]','[["label layered"|translate]]','[["label type"|translate]]',
                '[["label lights flash"|translate]]','[["label interface type"|translate]]',
                'RAID [["label cache"|translate]]','[["label disk cache"|translate]]',"磁盘序列号",
                'RAID [["label level"|translate]]','RAID [["label card type"|translate]]',"raid_missing"],
        colModel :[
            {name:'id', index:'id', width:30, align:'left',sortable:false,hidden:true},
            {name:'host_ip', index:'host_ip', width:100, align:'left',formatter:function(cellvalue,option,row) {
                return '<a href="#" onclick="detail_open(\'' + option.gid + '\',\'' + option.rowId + '\')">' + cellvalue + '</a>';
            },unformat:function(cellvalue) {
                return cellvalue;
            }},
            {name:'device', index:'device', width:80, align:'left',sortable:false},
            {name:'used', index:'used', width:40, align:'left',sortable:false,hidden:true},
            {name:'size', index:'size', width:100, align:'left',sortable:false},
            {name:'capacity', index:'capacity', width:10, align:'left',hidden:true},
            {% if is_fusionnas %}
                {name:'mountpoint', index:'mountpoint', width:90, align:'left'},
            {% else %}
                {name:'mountpoint', index:'mountpoint', width:10, align:'left',hidden:true},
            {% endif %}
            {name:'isjoin_show', index:'isjoin_show', width:80, align:'left',sortable:false,},
            {name:'isjoin', index:'isjoin', width:30, align:'left',sortable:false,hidden:true},
            {name:'host_id', index:'host_id', width:30, align:'left',sortable:false,hidden:true},
            {name:'islight', index:'islight', width:30, align:'left',sortable:false,hidden:true},
            {name:'stat', index:'stat', width:73, align:'left',sortable:false},
            {name:'enable_delete', index:'enable_delete', width:73, align:'left',sortable:false,hidden:true},
            {name:'disk_option', index:'disk_option', width:120, align:'left',sortable:false,detailHidden:true},
            {% if is_fusionnas %}
                {name:'tier', index:'tier', width:73, align:'left',hidden:true},  
                {name:'disk_type', index:'disk_type', width:73, align:'left',sortable:false,hidden:true},  //ssd hidden                
                {name:'disk_light', index:'disk_light', width:100, align:'left',sortable:false,hidden:true}, 
                {name:'interface_type', index:'interface_type', width:75, align:'left',sortable:false,hidden:true},
                {name:'raid_cache',index:'raid_cache',width:75,align:'left',sortable:false,hidden:true},    
                {name:'disk_cache',index:'disk_cache',width:75,align:'left',sortable:false,hidden:true},	 
                {name:'serial_number',index:'serial_number',width:100,align:'left',sortable:false,hidden:true}, 
                {name:'raid', index:'raid', width:75, align:'left',sortable:false,hidden:true},  	    
                {name:'disk_model',index:'disk_model',width:100,align:'left',sortable:false,hidden:true},
            {% else %}
                {name:'tier', index:'tier', width:73, align:'left'},  
                {name:'disk_type', index:'disk_type', width:73, align:'left',sortable:false,hidden:true},  //ssd hidden                
                {name:'disk_light', index:'disk_light', width:100, align:'left',sortable:false}, 
                {name:'interface_type', index:'interface_type', width:75, align:'left',sortable:false},
                {name:'raid_cache',index:'raid_cache',width:75,align:'left',sortable:false},    
                {name:'disk_cache',index:'disk_cache',width:75,align:'left',sortable:false},	 
                {name:'serial_number',index:'serial_number',width:100,align:'left',sortable:false,hidden:true}, 
                {name:'raid', index:'raid', width:75, align:'left',sortable:false},  	    
                {name:'disk_model',index:'disk_model',width:100,align:'left',sortable:false},
            {% endif %}
            {name:'raid_missing',index:'raid_missing',width:75,align:'left',sortable:false,hidden:true},
        ],
        page:1,
        rowNum:10,
        sortname: 'id',
        sortorder: 'desc',
        viewrecords: true,
        //caption: '节点列表',
        altRows:true,
        height:mainpanelHeight,
        autowidth:true,
        shrinkToFit:true,
        multiboxonly:true,
        multiselectWidth:'60px',
        beforeSelectRow:function(rowid, e) {
            if($(e.target).is('input')) {
                return true;
            }
            return false;
        },
        gridComplete:function(){
            var gridData = $(this).jqGrid('getRowData');
            jqgrid_page($(this), gridData);
            jqgrid_resize($(this));
            
            var ids=$(this).jqGrid('getDataIDs');
            var disk_fm="";
            var d2,d3="";
            var is_adding=false;
            var vgroup_ids = $(this).attr("id");
            //判断是否由盘加入中
            for(var i=0; i<ids.length; i++){ 
                if($(this).getCell(ids[i],'stat')=="加入中"){ 
                    is_adding=true;
                }
            }          
            //on server top==>not node del           
            for(var i=0; i<ids.length; i++){ 
                var is_deleting=false;
                if($(this).getCell(ids[i],'stat')=="删除中"){ 
                    is_deleting=true;
                }

                var disk_id = ids[i];
                var ID = ids[i];
                var disk_id = $(this).getCell(ID, "id");
                var host_id = $(this).getCell(ID, "host_id");
                var used = $(this).getCell(ID, "used");
                var disk_operation_source=new Array();
                var disk_model = $(this).getCell(ID, 'disk_model');
                var isjoin = $(this).getCell(ID, 'isjoin')
                var device = $(this).getCell(ID, 'device');
                var serial_number = $(this).getCell(ID, 'serial_number');
                var tier = $(this).getCell(ID, 'tier');
                var details_ip = $(this).getCell(ids[i], "host_ip");
                if (parseInt(tier) == 0 ) { 
                    $(this).jqGrid('setRowData', ID, {tier:'性能'});
                }
                if (parseInt(tier) == 1 ) { 
                    $(this).jqGrid('setRowData', ID, {tier:'容量'});
                }
                var sizes = "<span style='display:inline-block;text-align:right;'>"+used+"</span>% of "+ $(this).getCell(ID,"capacity");
                $(this).jqGrid('setRowData', ID, {size: sizes});

                var td_disk_model_width = $("#grid_disk_table_disk_model").width();
                if (disk_model != ''){
                    $(this).jqGrid('setRowData', ID, {disk_model: '<span class="td_disk_model" style="display:inline-block;text-overflow:ellipsis; white-space:nowrap;overflow:hidden;width:'+td_disk_model_width+'px;">'+disk_model+'</span>'}); 
                }
                raid = $(this).getCell(ID,'raid')
                if (isjoin == 'True'){
                    $(this).jqGrid('setRowData', ID, {size: ''});
                    disk_fm='<input id="disk_list_'+ID+'"  class="rounded_dropdown" readonly="readonly" style="width:100px;" value="操作" realvalue="add_cluster"/>'; 
                    $(this).jqGrid('setRowData', ID, {disk_option: disk_fm}); 
                    //加入中时操作列表为空
                    if($(this).getCell(ID,'stat')=="加入中"){
                          disk_fm='<input   class="btn btn-blue" type="button" disabled style="width:97px;text-align:left;padding-left:10px;" value="操作" />';                           
                          $(this).jqGrid('setRowData', ID, {disk_option: disk_fm});     
                    }else{
                        //判断是否有盘是加入中，有的话屏蔽其他盘的加入集群
                        if(!is_adding){
                            if($(this).getCell(ID, 'stat')=="正常"&&$(this).getCell(ID,'raid')==""&&$(this).getCell(ID,'disk_model')!=""){
                                disk_operation_source.push(["add_raid"+"*"+disk_id+"*"+device,"加入RAID"]); 
                            }     
                            //add_cluster   
                            if($(this).getCell(ID, 'stat') != "不正常"){
                                if($(this).getCell(ID,'raid')=="0"||$(this).getCell(ID,'disk_model')==""){
                                    disk_operation_source.push(["add_cluster"+"*"+disk_id+"*"+device,"加入集群"]); 
                                }
                            }
                            //clear_raid
                            if($(this).getCell(ID, 'disk_model')!="" && device != serial_number &&$(this).getCell(ID,'raid')=="0"){
                                    disk_operation_source.push(["clear_raid"+"*"+disk_id+"*"+device,"清除RAID"]);   
                            }                
                            if(disk_operation_source.length==0) {
                                disk_fm='<input   class="btn btn-blue" type="button" disabled style="width:97px;text-align:left;padding-left:10px;" value="操作" />';  
                                $(this).jqGrid('setRowData', ID, {disk_option: disk_fm});                 
                            }  
                            else{
                                $('#disk_list_'+ID).rqDropDown({
                                    "datatype":"local",
                                    "source": disk_operation_source,
                                    "onchange":"disk_list_fun"
                                });  
                            }
                        }
                    }
                }else{
                    if(is_deleting)
                        disk_fm="<input class='btn btn-blue' type='button' value='移除'  disabled style='width:97px' id='disk_remove' onclick=\"confirm_delete('"+disk_id+"',"+host_id+")\"/>";
                    else
                        disk_fm="<input class='btn btn-blue' type='button' value='移除' style='width:97px' onclick=\"confirm_delete('"+disk_id+"',"+host_id+")\"/>";                                                      
                    {% if is_fusionnas %}
                        disk_fm="<input class='btn btn-blue' type='button' value='移除' disabled style='width:97px' onclick=\"confirm_delete('"+disk_id+"',"+host_id+")\"/>";                                                      
                    {% endif %}

                    $(this).jqGrid('setRowData', ID, {disk_option: disk_fm});                                  
                }
                    disk_light="<a style='display:inline-block;width:62px;height:23px;cursor:pointer'   onclick=\"disk_light_fun(this,'"+disk_id+"',"+host_id+")\"><img src='/static/img/disk_light_close.png' alt='磁盘灯' width='62' height='23' /></a>";
                    var islight = $(this).getCell(ID, 'islight')
                    if (islight == 'on'){
                         disk_light="<a style='display:inline-block;width:62px;height:23px;cursor:pointer'   onclick=\"disk_light_fun(this,'"+disk_id+"',"+host_id+")\"><img src='/static/img/disk_light_open.png' alt='磁盘灯' width='62' height='23' /></a>";
                    }
                    $(this).jqGrid('setRowData', ID, {disk_light: disk_light});
                    
                    //Disabled Delete
                    var enable_delete = $(this).getCell(ID, 'enable_delete');
                    
                }//endfor
            }
            
        });
    function confirm_disk(disk_id, id){
        
         angular.element('#disk_modal_controller').scope().open('/disk/disk_add_confirm?disk_id='+disk_id);
    }

    function confirm_delete(disk_id,id){ 
        angular.element('#disk_modal_controller').scope().open('/disk/disk_delete_confirm?disk_id='+disk_id);
    }

    function disk_option(host_id, force, is_license_over){
        if (option == 'delete'){
            delete_disk(host_id,force)
        }
        if (option == 'add'){               
            add_disk(host_id, force, is_license_over)
        }
        if (option == 'clear'){
            clear_disk(host_id,force,0)
        }
    }

    function confirm_clear(disk_id, id){
       angular.element('#disk_modal_controller').scope().open('/disk/raid_delete_confirm?disk_id='+disk_id)
    }

    function disk_list_fun(name, status){
        disk_id = status.split('*')[1]
        disk = status.split('*')[2]
        if (status.substring(0, 11) == 'add_cluster'){ 
               if($("#disk_path").hasClass(disk_id+"add")){
                  $(".device_tip").text(disk_id+" 正在加入集群中，请稍候").css({"display":"block","color":'#000'});             		       
                  return false      
               }
               else if($("#disk_path").hasClass(disk_id+"clear")){
                  $(".device_tip").text(disk_id+" 正在清除RAID信息中，请在清除RAID操作完成后再对磁盘进行其他操作，以免引起其他问题").css({"display":"block","color":'#000'});             		                        
                  return false;             
               }
            confirm_disk(disk_id,"host_idq")
        }
        if (status.substring(0,10) == 'clear_raid'){
           if($("#disk_path").hasClass(disk_id+"clear")){
              $(".device_tip").text(disk_id+" 正在清除RAID信息中，请稍候").css({"display":"block","color":'#000'});             		       
              return false      
           }        
            confirm_clear(disk_id,"host_idq")
        }
        if(status.substring(0,8)=="add_raid"){
            if($("#disk_path").hasClass(disk_id+"add_raid")){
                $(".device_tip").text(disk_id+" 正在加入RAID中，请稍候").css({"display":"block","color":'#000'});             		       
                return false      
            }
           angular.element('#disk_modal_controller').scope().open('/disk/raid_add_confirm?disk_id='+disk_id)
        }     
    }
    function disk_light_fun(obj,disk,id){ 
        //判断是否有raid卡信息
        if($(obj).parent().siblings("[aria-describedby=grid_disk_table_device]").text()=="未知设备，请移除磁盘"){
           $(".device_tip").text("磁盘是空挂载目录，不支持磁盘灯闪烁").css({"display":"block","color":'#000'});             		               
           return false     
        }
        if($(obj).parent().siblings("[aria-describedby=grid_disk_table_disk_model]").text()=="无" && $(obj).parent().siblings("[aria-describedby=grid_disk_table_stat]").text()=="正常"){
           $(".device_tip").text("非RAID卡磁盘，不支持磁盘灯闪烁").css({"display":"block","color":'#000'});             		                      
          return false     
        }
        //start
        if($(obj).find("img").attr("src")=="/static/img/disk_light_close.png"){
           $.get('/disk/disk_light_option', {'disk_id':disk,'option':'start'}, function(data){
               if (data.reply.is_success){
                    $(obj).find("img").attr("src","/static/img/disk_light_open.png")
               } else {
                   error =  data.reply.error;
                   SelfAlert(error);
               }
           });
        }
        //stop
        if($(obj).find("img").attr("src")=="/static/img/disk_light_open.png"){               
           $.get('/disk/disk_light_option', {'disk_id':disk,'option':'stop'}, function(data){
               if (data.reply.is_success){
                    $(obj).find("img").attr("src","/static/img/disk_light_close.png")
               } else {
                   error =  data.reply.error;
                   $(".device_tip").text(error).css({"display":"block","color":'#000'});             		       
               }
           });
        }
       
    } 

    function raid_delete(disk_id, force, dialog_num){
        var is_join = $("#raid_delete_modal").find("#is_read_tip").is(':checked');

        var disk = $("#grid_disk_table").getCell(disk_id, 'device');
        angular.element('#disk_modal_controller').scope().close();
        var disk_row_id=$('#grid_disk_table').find("td[aria-describedby=grid_disk_table_device]:contains('"+disk+"')").parent().attr("id");
        $(".device_tip").text(disk+"磁盘RAID清空中，请耐心等待").css({"display":"block","color":'#000'});    
        $("#disk_path").addClass(disk+"clear");         		                       
        $.get('/disk/raid_delete', {'disk_id':disk_id,'force':force}, function(data){
            //disk = data.disk
            if (data.reply.is_success){
                tip = "磁盘RAID清空成功";
                $(".device_tip").text(tip).css({"display":"block","color":'#000'}); 
              	$("#rqDropDownList_disk_list_"+ disk_row_id).remove();
                $('#grid_disk_table').trigger("reloadGrid");
                $("#disk_path").removeClass(disk+"clear");
            } else {
                var error =  data.reply.error;
                if (isContains(error, "try use --force") || isContains(error, 'Read-only file system')) {
                    angular.element('#disk_modal_controller').scope().open('/disk/raid_force_delete_confirm?disk_id='+disk_id);
                } else {
                    $(".device_tip").text(error).css({"display":"block","color":'#000'});  
                    $("#disk_path").removeClass(disk+"clear");           		       
                    SelfAlert(error)
                }
            }
            $("#disk_path").removeClass(disk+"clear");           		       
        });
    } 
      function delete_disk(id, force){
      var is_join = $("#disk_remove_modal").find(".is_join_disk").is(':checked');
                var disk_id = $("#disk_remove_modal").find("#ready_disk").val();
                is_force = 'false'
                if (force){
                    is_force = 'true'
                }
                /*if (!is_join){
                    error = "请阅读以下信息"
                    $(".rtn-tip").text(error).slideDown();
                    return false
                }*/
                $("#disk_path").addClass("delete");
                $("#disk_path").addClass("call_delete");
                 $("#disk_path").attr("role",disk_id)
                angular.element('#disk_modal_controller').scope().close();
                disk_device = $('#grid_disk_table').getCell(disk_id, 'device')
                $(".device_tip").text(disk_device+"磁盘删除中，请耐心等待，可稍后查看磁盘状态确定删除情况").css({"display":"block","color":'#000'});             		                       
                $('#grid_disk_table').find("td[aria-describedby=grid_disk_table_id]").each(function(){
                   if($(this).text()==disk_id){
                      if($(this).siblings("td[aria-describedby=grid_disk_table_stat]").text()=="不正常")
                             {
                               $("#disk_path").removeClass("delete");
                               $("#disk_path").attr("role","");
                             }
                $(this).parent().siblings().find("td[aria-describedby=grid_disk_table_disk_option]").find("input[type=button]").attr("color","#f00")
            $(this).parent().siblings().find("td[aria-describedby=grid_disk_table_disk_option]").find("input[type=button]").attr("disabled","disabled") 
                      $(this).siblings("td[aria-describedby=grid_disk_table_disk_option]").find("input[type=button]").attr("disabled","disabled")                          
                      $(this).siblings("td[aria-describedby=grid_disk_table_server_option]").find("input[type=button]").attr("disabled","disabled")                                             
                   }
                })   
                $.get('/disk/disk_delete', {'disk_id':disk_id,'force':is_force}, function(data){
                    if (data.reply.is_success){
                        //tip = data.split('*')[1]
                        //if (tip){
                        //    $(".device_tip").text(tip).css({"display":"block","color":'#000'});             		       
                        //}
                        $("#disk_path").removeClass("delete");
	                $('#grid_disk_table').trigger("reloadGrid");
                    } else {
                        error =  data.reply.error;
                      $('#grid_disk_table').find("td[aria-describedby=grid_disk_table_id]").each(function(){
                        if($(this).text()==disk_id){
                          $(this).siblings("td[aria-describedby=grid_disk_table_server_option]").find("input[type=button]").removeAttr("disabled")                          
                          $(this).siblings("td[aria-describedby=grid_disk_table_disk_option]").find("input[type=button]").removeAttr("disabled")                          
                          $(this).parent().siblings().find("td[aria-describedby=grid_disk_table_disk_option]").find("input[type=button]").removeAttr("disabled") 
                       }
                       })  
                        $("#disk_path_").removeClass("call_delete");
                       $("#disk_path").removeClass("delete");	      
                       $("#disk_path").attr("role","");
	                    $('#grid_disk_table').trigger("reloadGrid");                       
                       $(".device_tip").text(error).css({"display":"block","color":'#000'});       
                    }
                });
  }
    var refreshdisk;

    function add_disk(disk_id, is_force){
        var postData = {'disk_id':disk_id,is_force:is_force}
        $.post('/disk/disk_add', postData,
            function(data){
            if (data.reply.is_success){
                var a = 1
            } else {
                var error =  data.reply.error;
                $(".device_tip").text(error).css({"display":"block","color":'#000'});       
            }
            angular.element('#disk_modal_controller').scope().close();
            $('#grid_disk_table').trigger("reloadGrid");
        });
    }

    function freshdisk(){
        $('#grid_disk_table').trigger("reloadGrid");
        clearInterval(refreshdisk)
    }

    function select_hosts(tg, id){
        $("#grid_disk_table").jqGrid('setGridParam', {page:1, url:'/disk/grid?host_id='+ id}).trigger('reloadGrid');
    }

    var host_source = [['','全部']];
    {% for host in hosts %}
        var values = new Array();
        values[0] = "{{ host.id }}";
        values[1] = "{{host.ip}}";
        host_source.push(values)
    {% endfor %}

    $('#disk_search_with_host').rqDropDown({
        "datatype":"local",
        "source":host_source,
        "onchange":"select_hosts"
    }); 
  </script>

<div class="panel panel-f-style">
    <div class="panel-body">
        <div class="title-tag">
            <h3>磁盘</h3>
            <p>磁盘管理</p>
        </div>
        <div class="grid-tools">
            <!-- <div class="grid-search-down-arrow">
                <i class="fa fa-angle-down"></i>
            </div> -->
            <div class="grid-operate pull-left">
                <!--<button class="btn btn-blue" type="button"><i class="glyphicon glyphicon-trash"></i>移除</button>-->
            </div>
        </div>
        <div class="grid-body" id="gridttt">
            <table id="grid_disk_table"></table>
        </div>
    </div>
</div>




<!-- <div class="panel panel-default">
     <div class="panel-body">
         <label id="disk_path_"></label>
         <div class="item_toolsbar">
             <input id="disk_search_with_host"  class="rounded_dropdown" readonly="readonly"  style="width:150px;" value="按节点查看" realvalue=''/>
             <button id="snapshot_create" ng-controller="SnapshotModalCtrl" ng-show="false"></button>
             <span class="message_img">&nbsp;</span>
         </div>
             <div class="error_tip error_des device_tip"></div>
         <div id="div_disk_table" class='tab_warp'>
              <table id="grid_disk_table"></table>
         </div>
 
     </div>
 </div> --> <!-- panel-default -->
